package main

import (
	"fmt"
	"gitee.com/golang_example/src/muke/functions"
	"gitee.com/golang_example/src/muke/mock"
	"gitee.com/golang_example/src/muke/real"
	"time"
)

type Retriever interface {
	Get(url string) string
}

// type Processes interface {
// 	Process(array []int, elem func()) error
// }
//
// func Process(p Processes, array []int, elem func()) error {
// 	return p.Process(array, elem)
// }

func Download(r Retriever) string {
	return r.Get("https://www.imooc.com")
}

func main() {
	var r Retriever
	// url := "https://www.imooic.com"
	r = mock.Retriever{"this is fake imooc.com!"}
	r = real.Retriever{UserAgent: "Mozilla/5.0", TimeOut: time.Minute}

	fmt.Printf("%T %v", r, r)
	// fmt.Println(Download(r))
	// 斐波那契函数调用
	functions.PrintFibo()
	// 累加调用
	functions.PrintSum()

	intSlice := make([]int, 0)
	intSlice = append(intSlice, 3, 1, 4, 2)

	fmt.Printf("这是倒序:")
	functions.Process(intSlice, functions.SortByDescending)
	fmt.Println(intSlice)

	fmt.Printf("这是正序：")
	functions.Process(intSlice, functions.SortByAscending)
	fmt.Println(intSlice)

	stringSlice := make([]string, 0)
	stringSlice = append(stringSlice, "hello", "world", "china")

	// 使用匿名函数覆盖原始函数
	functions.Process(stringSlice, func(elem interface{}) {
		if slice, ok := elem.([]string); ok {
			for index, value := range slice {
				fmt.Println("index:", index, "  value:", value)
			}
		}
	})

	fmt.Printf("\n这是字符串匿名函数自定义排序:%v\n", stringSlice)

	floatSlice := make([]float32, 0)
	floatSlice = append(floatSlice, 1.2, 2.4, 4.2, 3.5)

	// 使用匿名函数覆盖原始函数
	functions.Process(floatSlice, func(elem interface{}) {
		if slice, ok := elem.([]float32); ok {
			for index, value := range slice {
				slice[index] = value
			}
		}
	})

	var p functions.Processes

	p.IntSlice = intSlice
	p.FloatSlice = floatSlice
	p.SortByAsc = "asc"

	fmt.Printf("\n这是浮点型匿名函数自定义排序:%v\n", floatSlice)

	sliceEven := make([]int, 0)
	sliceEven = append(sliceEven, 1, 2, 3, 4)
	sliceEven = functions.Data(sliceEven, functions.EventFilter).([]int)
	fmt.Printf("这是奇数变偶数:%v\n", sliceEven)
}
